<template>
	<view class="login_box">
		<view class="box_header">
			<i class="iconfont icon-meishi" style="font-size: 60rpx; color: #cde6c7;"></i>
			<text>广东美食</text>
		</view>
		<view class="box_center">
			<u--input placeholder="请输入账号" border="bottom" clearable v-model="phone"></u--input>
			<u--input placeholder="请输入密码" border="bottom" clearable type="password" v-model="password"></u--input>
			<u-button text="登录" color="linear-gradient(to right, rgb(220, 194, 11), rgb(4, 151, 99))"
				@click="userLogin"></u-button>
		</view>
		<view class="box_footer">
			<u-checkbox-group>
				<u-checkbox labelSize="13" :checked="checked" @change="checkSelect" shape="circle" label="管理员"
					ref='admin'></u-checkbox>
			</u-checkbox-group>
			<!-- <text>忘记密码</text> -->
			<text @click="toRegister">注册账号</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isRegister: true,
				phone: "",
				password: "",
				checked: false
			}
		},
		methods: {
			toRegister() {
				this.$emit("isLogin", this.isRegister)
			},
			userLogin() {
				uni.request({
					url: 'http://localhost:8081/user/login',
					method: 'POST',
					data: {
						phone: this.phone,
						password: this.password
					},
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					dataType: 'json',

					success: (res) => {
						let isChecked = this.checked
						if (res.data.state == 200) {
							if (isChecked == true && res.data.data.role == 1) {
								//#ifndef MP-WEIXIN
								uni.showToast({
									title: '登录成功',
									duration: 1000,
								});
								try {
									uni.setStorageSync('token', res.data.data.id);
									console.log('1',res.data.data)
									this.$store.dispatch('asyncUpdateUser',{username: res.data.data.username})
								} catch (e) {
									// error
								}
								setTimeout(function() {
									uni.redirectTo({
										url: '../../pages/admin/admin'
									})
								}, 1000);
								//#endif 
							} else if (isChecked == false && res.data.data.role == 0) {
								uni.showToast({
									title: '登录成功',
									duration: 1000,
								});
								try {
									uni.setStorageSync('token', res.data.data.id);
									console.log('2', res.data.data)
									this.$store.dispatch('asyncUpdateUser',{username:res.data.data.username})
								} catch (e) {
									// error
								}
								setTimeout(function() {
									uni.switchTab({
										url: '../../pages/home/home'
									})
								}, 1000);
							} else {
								uni.$u.toast("用户不存在")
							}
						} else {
							uni.$u.toast(res.data.message)
						}
					}
				});
			},
			checkSelect() {
				this.checked = !this.checked
			}
		}
	}
</script>

<style lang="scss">
	.login_box {
		width: 500rpx;
		height: 400rpx;
		background: #FFFFFF;
		opacity: 0.7;
		margin-top: -150rpx;
		padding: 40rpx 50rpx;

		.box_header {
			text {
				font-size: 50rpx;
				margin-left: 20rpx;
			}
		}

		.box_center {
			margin-top: 30rpx;

			uni-button {
				margin-top: 30rpx;
				margin-bottom: 10rpx;
			}
		}

		.box_footer {
			display: flex;
			justify-content: space-between;

			text {
				font-size: 20rpx;
			}
		}
	}
</style>
